var primePools = [];
var primes = [];

function getPrimes(limit) {
    var sqrt = Math.floor(Math.sqrt(limit));
    primePools[1] = 1;
    primePools[2] = 0;
    for(var i=2; i<=sqrt; i++) {
        for(var j=i*2; j<=limit; j=j+i) {
            primePools[j] = 1;
        }
    }
}

function isPrime(num) {
    return primePools[num] != 1
}

print('get primes.....');
var t=new Date().getTime();
var plimit = 1000000
getPrimes(plimit);
for(var i=2; i<=plimit; i++) {
    if(isPrime(i)) primes.push(i);
}
print('get primes done..... ' + ((new Date().getTime()-t)/1000) + "s");

var count = 0;
var tmp0, tmp1, tmp2, base=10, size=0;
for(var i=0; i<primes.length; i++) {
    if(primes[i]<10) {
        count++;
        print(primes[i]);
        continue;
    }
    if(primes[i]>base) {
        base = base*10;
        size = size+1;
    }
    
    if(primes[i]>10) {
        var pass=true;
        tmp = primes[i];
        //print("prime=" + primes[i] + ",size=" + size + ",base=" + base);
        for(j=0; j<size; j++) {
            tmp0 = tmp%10;
            tmp1 = (tmp-tmp0)/10;
            tmp  = base*tmp0/10+tmp1
            //print("test tmp=" + tmp)
            if(!isPrime(tmp)) {
                pass=false;
                break;
            }
        }
        if(pass) {
            count++;
            print(primes[i]);
        }
    }
}

print("count=" + count);
